package cn.itcast.service.impl;


import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Role;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize,String name) {
        //开启分页查询
        PageHelper.startPage(currentPage,pageSize);
        //根据条件分页查询检查项信息
        Page<Role> pages = roleDao.selectByCondition(name);
        //构建分页数据返回
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Transactional
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //添加

        roleDao.add(role);
        if (menuIds!=null&&menuIds.length>0&&menuIds[0]!=null){
            for (Integer menuid : menuIds) {
                //添加角色id与菜单id关联
                roleDao.addroleandmenu(role.getId(),menuid);
            }
        }

        if (permissionIds!=null&&permissionIds.length>0 && permissionIds[0] != null){
            for (Integer permissionid : permissionIds) {
                //添加角色id与权限id关联
                roleDao.addroleandpermissionid(role.getId(),permissionid);
            }
        }

//        if (menuId!=null&&permissionId!=null){
//            if (menuId.length==0&&permissionId.length==0){
//                roleDao.add(role);
//            }else {
//                if (menuId.length>0){
//                    for (Integer menuid : menuId) {
//                        //添加角色id与菜单id关联
//                        roleDao.addroleandmenu(role.getId(),menuid);
//                    }
//                }
//                if (permissionId.length>0){
//                    for (Integer permissionid : permissionId) {
//                        //添加角色id与权限id关联
//                        roleDao.addroleandpermissionid(role.getId(),permissionid);
//                    }
//                }
//
//                roleDao.add(role);
//            }
//        }else {
//            roleDao.add(role);
//        }

    }


    //根据ID查询角色基本信息
    @Override
    public Role findById(Integer id) {
       return roleDao.findById(id);

    }

    @Override
    public void update(Role role,Integer[] permissionIds,Integer[] menuIds) {
        roleDao.update(role);

        //根据id删除权限ID
        Integer[] integers = roleDao.selectpermissionById(role.getId());
        if (integers!=null&&integers.length>0 && integers[0] != null){
                roleDao.deletePermissionIds(role.getId());
        }

        //根据用户ID删除菜单ID
        Integer[] integers1 = roleDao.selectMenuById(role.getId());
        if (integers1!=null&&integers1.length>0&&integers1[0]!=null){

                roleDao.deleteMenuIds(role.getId());

        }

        //根据id添加权限ID
        if (permissionIds!=null&&permissionIds.length>0 && permissionIds[0] != null){
            for (Integer permissionid : permissionIds) {
                //添加角色id与权限id关联
                roleDao.addroleandpermissionid(role.getId(),permissionid);
            }
        }


        //根据id添加菜单ID
        if (menuIds!=null&&menuIds.length>0&&menuIds[0]!=null){
            for (Integer menuid : menuIds) {
                //添加角色id与菜单id关联
                roleDao.addroleandmenu(role.getId(),menuid);
            }
        }
    }

    //根据角色id删除
    @Override
    public void deleteById(Integer id) {
        //判断是否被关联
        int i = roleDao.selectormenu(id);
        int i1 = roleDao.selectorpermission(id);

        System.out.println(i+"+"+i1);
        if (i>0||i1>0){
            throw new RuntimeException("当前检查项被引用，不能删除 !");
        }
        roleDao.deleteById(id);
    }

    //根据ID查询关联菜单id
    @Override
    public Integer[]  menuById(Integer id) {
        Integer[] arr=roleDao.selectMenuById(id);
        return arr;
    }

    //查询关联权限ID
    @Override
    public Integer[]  permissionById(Integer id) {
        Integer[] arr=roleDao.selectpermissionById(id);
        return arr;
    }

}
